﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using ESOL.Common.Entity;
using ESOL.Common;
using ESOL.Business.Sales;
using ESOL.Business.Mst;
using ESOL.Business.Fin;

namespace ESOL.WinUI.Finance
{
    public partial class frmMaintainReturnAccountOrder : BaseForm.frmBaseDialogyBusinessForm
    {
        #region Vars
        public List<SalesReturnItemEntity> SelectedReturnItems{ get; set; }
        public FinReturnAccountOrderEntity SelectedFinReturnAccountOrder { get; set; }
        #endregion

        #region Cons
        public frmMaintainReturnAccountOrder()
        {
            InitializeComponent();
        }
        #endregion


        #region Func
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "FRAO", true);
        }
        private void SetField()
        {
            if (SelectedFinReturnAccountOrder == null)
            {
                return;
            }
            txtReturnAccountOrder.Text = SelectedFinReturnAccountOrder.OrderCode;
            SalesReturnOrderEntity returnOrder = SalesReturnOrderBs.GetSalesReturnOrderById(SelectedFinReturnAccountOrder.SalesReturnOrderId);
            if(returnOrder!=null)
            {
                txtReturnOrder.Text = returnOrder.OrderCode;
                txtReturnOrder.Tag = returnOrder;
                SalesProdPlanApplicationEntity ProdApp = SalesProdPlanApplicationBs.GetSalesProdPlanApplicationByID(returnOrder.ApplicationId);
                if (ProdApp != null)
                {
                    txtApplicationCode.Text = ProdApp.ApplicationCode;
                    txtApplicationCode.Tag = ProdApp;
                }
            }
            MstCustomerEntity Customer = MstCustomerBs.GetMstCustomerByID(SelectedFinReturnAccountOrder.CustomerId);
            if (Customer != null)
            {
                txtCustomer.Text = Customer.CustomerName;
                txtCustomer.Tag = Customer;
            }
            txtTotalAmount.Text = SelectedFinReturnAccountOrder.TotalAmount.ToString();
            txtRemark.Text = SelectedFinReturnAccountOrder.Remark;
            dtpOrderDate.EditValue = SelectedFinReturnAccountOrder.OrderDate;
            cmbStatus.SelectedValue = SelectedFinReturnAccountOrder.Status;

            UICommon.UICommonFunctions.SetTextBoxPersonName(txtCreatedBy, SelectedFinReturnAccountOrder.CreatedBy);
            UICommon.UICommonFunctions.SetTextBoxPersonName(txtModifiedBy, SelectedFinReturnAccountOrder.ModifiedBy);
            UICommon.UICommonFunctions.SetTextBoxPersonName(txtApprovedBy, SelectedFinReturnAccountOrder.ApprovedBy);
            UICommon.UICommonFunctions.SetTextBoxDate(txtCreated, SelectedFinReturnAccountOrder.Created, "yyyy-MM-dd hh:mm:ss");
            UICommon.UICommonFunctions.SetTextBoxDate(txtModified, SelectedFinReturnAccountOrder.Modified, "yyyy-MM-dd hh:mm:ss");
            UICommon.UICommonFunctions.SetTextBoxDate(txtApproved, SelectedFinReturnAccountOrder.Approved, "yyyy-MM-dd hh:mm:ss");
        }
        private void SetFieldByItemList()
        {
            if (SelectedReturnItems == null||SelectedReturnItems.Count==0)
            {
                return;
            }
            SalesReturnOrderEntity returnOrder=SalesReturnOrderBs.GetSalesReturnOrderById(SelectedReturnItems[0].SalesReturnOrderId);
            if(returnOrder!=null)
            {
                txtReturnOrder.Text = returnOrder.OrderCode;
                txtReturnOrder.Tag = returnOrder;
                SalesProdPlanApplicationEntity prodApp = SalesProdPlanApplicationBs.GetSalesProdPlanApplicationByID(returnOrder.ApplicationId);
                if (prodApp != null)
                {
                    txtApplicationCode.Text = prodApp.ApplicationCode;
                    txtApplicationCode.Tag = prodApp;
                }
                MstCustomerEntity Customer = MstCustomerBs.GetMstCustomerByID(returnOrder.CustomerId);
                if (Customer != null)
                {
                    txtCustomer.Text = Customer.CustomerName;
                    txtCustomer.Tag = Customer;
                }
            }
            cmbStatus.SelectedValue=Common.ReturnAccountOrder.FRAO01.ToString();
            decimal totalAmount=0;
            foreach (SalesReturnItemEntity entity in SelectedReturnItems)
            {
                SalesProdPlanApplicationItemEntity item=SalesProdPlanApplicationItemBs.GetProdPlanApplicationItemByID(entity.SalesProdPlanApplicationItemId);
                totalAmount += Convert.ToDecimal(entity.ReturnQty) * item.Price;
            }
            txtTotalAmount.Text = Convert.ToString(Convert.ToDecimal(totalAmount));
            
        }

        private void SetEntity()
        {
            if (SelectedFinReturnAccountOrder == null || SelectedFinReturnAccountOrder.Id == null)
            {
                SelectedFinReturnAccountOrder = new FinReturnAccountOrderEntity();
                SelectedFinReturnAccountOrder.Id = Guid.NewGuid().ToString();
            }
            if (txtReturnOrder.Tag != null && txtReturnOrder.Tag.GetType() == typeof(SalesReturnOrderEntity))
            {
                SelectedFinReturnAccountOrder.SalesReturnOrderId = ((SalesReturnOrderEntity)txtReturnOrder.Tag).Id;
            }
            if (txtCustomer.Tag != null && txtCustomer.Tag.GetType() == typeof(MstCustomerEntity))
            {
                SelectedFinReturnAccountOrder.CustomerId = ((MstCustomerEntity)txtCustomer.Tag).CustomerId;
            }

            if (txtApplicationCode.Tag != null && txtApplicationCode.Tag.GetType() == typeof(SalesProdPlanApplicationEntity))
            {
                
            }
            if (dtpOrderDate.EditValue != null)
            {
                SelectedFinReturnAccountOrder.OrderDate = Convert.ToDateTime(dtpOrderDate.EditValue);
            }

            SelectedFinReturnAccountOrder.TotalAmount = Convert.ToDecimal(txtTotalAmount.Text);
            SelectedFinReturnAccountOrder.Remark = txtRemark.Text;

            if (ThisActionType == ActionType.New)
            {
                SelectedFinReturnAccountOrder.Status = Common.ReturnAccountOrder.FRAO01.ToString();
                SelectedFinReturnAccountOrder.CreatedBy = CommonParameter.CurrentUser.PersonId;
                SelectedFinReturnAccountOrder.Created = DateTime.Now;
                SelectedFinReturnAccountOrder.ModifiedBy = CommonParameter.CurrentUser.PersonId;
                SelectedFinReturnAccountOrder.Modified = DateTime.Now;
            }
            if (ThisActionType == ActionType.Modify)
            {
                SelectedFinReturnAccountOrder.Status = Common.ReturnAccountOrder.FRAO02.ToString();
                SelectedFinReturnAccountOrder.ModifiedBy = CommonParameter.CurrentUser.PersonId;
                SelectedFinReturnAccountOrder.Modified = DateTime.Now;
            }
        }
        private bool CheckDate()
        {
            if (string.IsNullOrEmpty(txtReturnOrder.Text))
            {
                MessageBox.Show("退货账款单号不能为空","Information",MessageBoxButtons.OK,MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtApplicationCode.Text))
            {
                MessageBox.Show("生产计划申请单号不能为空", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (string.IsNullOrEmpty(txtCustomer.Text))
            {
                MessageBox.Show("客户不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            if (dtpOrderDate.EditValue == null)
            {
                MessageBox.Show("单据时间不能为空！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
            return true;
        }
        private bool Save()
        {
            try
            {
                if (CheckDate()==false)
                {
                    return false;
                }
                if (ThisActionType == ActionType.New)
                {
                    SetEntity();
                    bool isSuccessful = FinReturnAccountOrderBs.SaveReturnAccountOrder(SelectedFinReturnAccountOrder, true);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("新增失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                if (ThisActionType == ActionType.Modify)
                {
                    SetEntity();
                    bool isSuccessful = FinReturnAccountOrderBs.SaveReturnAccountOrder(SelectedFinReturnAccountOrder, false);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("修改失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                if (ThisActionType == ActionType.Approve)
                {
                    SetEntity();
                    SelectedFinReturnAccountOrder.Approved = DateTime.Now;
                    SelectedFinReturnAccountOrder.ApprovedBy = CommonParameter.CurrentUser.PersonId;
                    string msg = "";
                    bool isSuccessful = FinReturnAccountOrderBs.ApproveReturnAccountOrder(SelectedFinReturnAccountOrder, msg);
                    if (!isSuccessful)
                    {
                        MessageBox.Show("审核失败", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        return false;
                    }
                }
                return true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }
        #endregion


        private void frmMaintainReturnAccountOrder_Load(object sender, EventArgs e)
        {
            BindComboBox();
            SetField();
            SetFieldByItemList();
        }
        protected override void Click_Save()
        {
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        protected override void Click_Approve()
        {
            SelectedFinReturnAccountOrder.Status = Common.ReturnAccountOrder.FRAO03.ToString();
            if (Save())
            {
                this.DialogResult = DialogResult.OK;
            }
        }

        protected override void Click_Reject()
        {
            SelectedFinReturnAccountOrder.Status = Common.ReturnAccountOrder.FRAO04.ToString();
            SelectedFinReturnAccountOrder.Approved = DateTime.Now;
            SelectedFinReturnAccountOrder.ApprovedBy = CommonParameter.CurrentUser.PersonId;
            
            bool isSuccessful = FinReturnAccountOrderBs.UpdateFinReturnAccountOrder(SelectedFinReturnAccountOrder);
            if (isSuccessful)
            {
                this.DialogResult = DialogResult.OK;
            }

        }
    }
}